package com.xmut.mall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.xmut.mall.member.annotation.CheckJwt;
import com.xmut.mall.member.searchparam.MemberComponentsSearchParam;
import com.xmut.mall.member.utils.RespBean;
import com.xmut.mall.member.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.member.entity.MemberEntity;
import com.xmut.mall.member.service.MemberService;
import com.xmut.mall.member.utils.PageUtils;
import com.xmut.mall.member.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 会员
 *
 * @author 1912120131suntingxing
 * @email 1192081604@qq.com
 * @date 2022-07-14 01:20:46
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 获取商城官方管理端的会员选择组件的分页数据
     * @param param
     * @return
     */
    @CheckJwt
    @PostMapping("/getManagerMemberComponentsPage")
    public RespBean getManagerMemberComponentsPage(@RequestBody MemberComponentsSearchParam param){
        PageVO vo = new PageVO();
        BeanUtils.copyProperties(param,vo);
        return memberService.getManagerMemberComponentsPage(vo,param);
    }

    /**
     * 根据用户id修改密码
     * @param vo
     * @return
     */
    @CheckJwt
    @PostMapping("/editPwd")
    public RespBean editPwd(@RequestBody EditPassWordVo vo){
        Boolean result = memberService.editPwd(vo);
        if(result){
            // 修改成功
            return RespBean.success("修改密码成功");
        }else {
            return RespBean.error("密码错误,请核对后重试");
        }
    }

    /**
     * 检查该用户是否设置了密码
     * @return
     */
    @CheckJwt
    @GetMapping("/checkPwdStatus/{userId}")
    public RespBean checkPwdStatus(@PathVariable Long userId){
        Boolean status =  memberService.checkPwdStatus(userId);
        if(status){
            // 如果他有密码 进入页面做的应该是修改密码
            return RespBean.success("修改密码");
        }else {
            // 如果他没设置密码 进入页面就应该是设置密码
            return RespBean.error("设置密码");
        }
    }

    /**
     * 修改用户个人信息
     * @param vo
     * @return
     */
    @CheckJwt
    @PutMapping("/editUserInfo")
    public RespBean editUserInfo(@RequestBody EditMemberVo vo){
        MemberEntity member = memberService.editUserInfo(vo);
        member.setPassword(null);
        return RespBean.success("用户信息修改成功",member);
    }

    @GetMapping("/testFeignUserAgent")
    public RespBean testFeignUserAgent(){
        System.out.println("现在的UserAgent:"+request.getHeader("User-Agent"));
        return RespBean.success("获取成功",request.getHeader("User-Agent"));
    }

    /**
     * 根据手机号码重置密码
     * @param vo
     * @return
     */
    @PostMapping("/resetPassword")
    public RespBean resetPassword(@RequestBody ResetPasswordVo vo){
        return memberService.resetPassword(vo);
    }

    /**
     * 根据手机号码查询用户
     * @param phone
     * @return
     */
    @GetMapping("/getUserByPhone/{phone}")
    public RespBean getUserByPhone(@PathVariable String phone){
        return memberService.getUserByPhone(phone);
    }

    /**
     * 用户登录
     * @param vo
     * @return
     */
    @PostMapping("/login")
    public RespBean login(@RequestBody LoginVo vo){
        return memberService.login(vo);
    }

    /**
     * 注册用户
     * @param vo
     * @return
     */
    @PostMapping("/registerUser")
    public RespBean registerUser(@RequestBody RegisterVo vo){
        return memberService.registerUser(vo);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
